package com.primes.functions;

import java.util.TreeMap;

import com.primes.functions.meta.Type;
import com.primes.util.ObjectCache;

public class Prime implements Type{

	 static Class<?> functionType;

	
	/**
	 * return TreeMap of first n primes. 
	 * @param n
	 * @return
	 */
	public static TreeMap<Long,Long> primes(int n){
		TreeMap<Long,Long> ret= new TreeMap<Long,Long>();
		int num=2;
		ret.put(1L, 2L);
		for(int i=1;i<n;){
			num++;
			boolean isPrime=true;
			for(int j=2;j<=num/2;j++){
				if(num%j==0){
					isPrime=false;
					break;
				}
			}
			if(isPrime)
				ret.put(new Long(++i),new Long(num));
		}
		
		return ret;
	}
	
	/**
	 * return nth prime
	 * @param n
	 * @return
	 * @throws Exception 
	 */
	public static Long prime(int n) throws Exception{
		TreeMap<Long,Long>  cached=  ObjectCache.getObjectFromCache(Prime.class);
		return cached.get(new Long(n));
	}
	
	

}
